package com.google.codejam.core.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * Writer to write the results of solving test cases to the output. This
 * object uses a <code>BufferedWriter</code> internally.
 */
public class TestCaseOutputWriter
{
	private final BufferedWriter buffered_writer;

	public TestCaseOutputWriter(OutputStream out)
	{
		buffered_writer =
				  new BufferedWriter(new OutputStreamWriter(out));
	}

	public TestCaseOutputWriter(
			  OutputStream out,
			  Charset charset)
	{
		buffered_writer =
				  new BufferedWriter(new OutputStreamWriter(out, charset));
	}

	public TestCaseOutputWriter(
			  OutputStream out,
			  int buffer_size)
	{
		buffered_writer =
				  new BufferedWriter(new OutputStreamWriter(out), buffer_size);
	}

	public TestCaseOutputWriter(
			  OutputStream out,
			  Charset charset,
			  int buffer_size)
	{
		buffered_writer =
				  new BufferedWriter(new OutputStreamWriter(out, charset),
				                     buffer_size);
	}

	public void write(String line_to_write)
			  throws IOException
	{
		buffered_writer.write(line_to_write);
	}

	public void writeLine(String line_to_write)
			  throws IOException
	{
		write(line_to_write);
		newLine();
	}

	public void newLine()
			  throws IOException
	{
		buffered_writer.newLine();
	}

	public void close()
			  throws IOException
	{
		if (buffered_writer != null)
		{
			buffered_writer.close();
		}
	}

	public void finalize()
			  throws Throwable
	{
		if (buffered_writer != null)
		{
			buffered_writer.close();
		}

		super.finalize();
	}
}
